package cn.wawi.dao;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Component;

@Component("dao")
public class DaoSupport{

	@Resource
	SqlSessionTemplate sqlSessionTemplate;
	
	public <T> List<T> findAll(String mapperName,Map<String, Object> map) {
		return sqlSessionTemplate.selectList(mapperName+".findAll",map);
	}

	public Object view(String mapperName,Object t) {
		if(t==null){
			throw new RuntimeException("view: object is null");
		}
		return sqlSessionTemplate.selectOne(mapperName+".view",t);
	}
	public void save(String mapperName,Object t) {
		if(t==null){
			throw new RuntimeException("save: object is null");
		}
		sqlSessionTemplate.insert(mapperName+".save",t);
	}
	public void delete(String mapperName,String id) {
		if(id==null){
			throw new RuntimeException("delete: id is null");
		}
		sqlSessionTemplate.delete(mapperName+".delete",id);
	}
	public void update(String mapperName,Object t) {
		if(t==null){
			throw new RuntimeException("update: object is null");
		}
		sqlSessionTemplate.update(mapperName+".update",t);
	}
	public void deleteBatch(String mapperName,String ids) {
		if(ids==null){
			throw new RuntimeException("deleteBatch: ids is null");
		}
		sqlSessionTemplate.delete(mapperName+".deleteBatch",ids.split(","));
	}
	public void updateBatch(String mapperName,String ids){
		if(ids==null){
			throw new RuntimeException("updateBatch: ids is null");
		}
		sqlSessionTemplate.delete(mapperName+".updateBatch",ids.split(","));
	}

}
